<?php

/**
 * AmplifyCMS
 *
 * This file is part of AmplifyCMS.
 *
 * AmplifyCMS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * AmplifyCMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 * Amplify Action Controller.
 * 
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */
class AmpActionController extends ActionController
{

  /**
   * 
   * @var OutletDAO
   */
  protected $db;

  /**
   * Text domain for translation.
   *
   * @var string
   */
  protected $domain;

  /**
   *
   * @var IndexOptionsComponent
   */
  protected $indexOptions;

  /**
   * 
   * @return void
   */
  public function buildMenu()
  {
    //
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/controller/ActionController#initialize()
   */
  protected function initialize()
  {
    parent::initialize();
    
    $this->db = OutletDAO::getInstance();
    
    if (! $this->request->isJSON()) {
      $file = sy_fix_path($this->getPath() . '/' . $this->getName() . '_actions_helper.php');
      if (file_exists($file)) {
        require_once ($file);
        $class = Inflector::camelize($this->getName() . '_actions_helper');
        $this->response->set('actions', new $class());
      }
      else {
        $this->response->set('actions', new AmpCommonActionsHelper());
      }
    }
  }

  /**
   *
   * @return void
   */
  public function loadLanguageFile()
  {
    Simplify::getL10n()->loadLanguageFile(null, $this->getTextDomain(), $this->getPath() . '/language/');
  }

  /**
   * Get text domain for this controller
   * 
   * @return string
   */
  public function getTextDomain()
  {
    if (empty($this->domain)) {
      $this->domain = $this->getName();
    }
    
    return $this->domain;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/controller/ActionController#action_index()
   */
  protected function action_index()
  {
    parent::action_index();
    
    $this->indexOptions = new AmpIndexComponent($this->app);
    $this->response->copyAll($this->indexOptions);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/controller/ActionController#beforeAction($action, $params)
   */
  protected function beforeAction($action, $params)
  {
    Simplify::getL10n()->setTextDomain($this->getTextDomain());
    
    parent::beforeAction($action, $params);
    
    $route = implode('/', array_merge(array(
      $this->getRoute(), $this->getName(), $action 
    ), $params));
    
    if (! Simplify::getInstance()->isUnrestricted($route) && ! Simplify::getInstance()->getUser()->can($route)) {
      throw new SecurityException(__('Permission denied'), SecurityException::PERMISSION_DENIED);
    }
  }

  protected function afterAction($action, $params, $output)
  {
    parent::afterAction($action, $params, $output);
    
    Simplify::getL10n()->setTextDomain();
  }

  /**
   * Validate an id. Id must be unsigned int.
   *
   * @param uint $id The id.
   * @throws ParameterException
   * @return void
   */
  protected function validateId($id)
  {
    $id = ( int ) $id;
    if (empty($id) || $id <= 0)
      throw new ParameterException('Invalid ID');
  }

}

?>